package team119;

import battlecode.common.Clock;
import battlecode.common.Direction;
import battlecode.common.GameActionException;
import battlecode.common.GameActionExceptionType;
import battlecode.common.MapLocation;
import battlecode.common.RobotController;
import battlecode.common.Team;

public class DefenseSoldierMovement
{
	public static void RunBot(RobotController rc,BOTInstanceVariables instanceVariables) throws GameActionException
	{
		if (rc.isActive()) 
		{
				instanceVariables.hqLoc = rc.getLocation().directionTo(rc.senseHQLocation());
				
				if(rc.senseMine(rc.getLocation())!=null 
						&& rc.senseMine(rc.getLocation()) != rc.getTeam())
				{

					rc.defuseMine(rc.getLocation());
					return;
				}
				
				if(rc.senseMine(rc.getLocation()) == rc.getTeam() || Clock.getRoundNum() < 4)
				{
					
					try {
						
						switch(instanceVariables.hqLoc)
						{
						case SOUTH:
							instanceVariables.moveDirection = Direction.NORTH;
							break;
						case SOUTH_EAST:
							instanceVariables.moveDirection = Direction.NORTH_WEST;
							break;
						case EAST:
							instanceVariables.moveDirection = Direction.WEST;
							break;
						case NORTH_EAST:
							instanceVariables.moveDirection = Direction.SOUTH_WEST;
							break;
						case NORTH:
							instanceVariables.moveDirection = Direction.SOUTH;
							break;
						case NORTH_WEST:
							instanceVariables.moveDirection = Direction.SOUTH_EAST;
							break;
						case WEST:
							instanceVariables.moveDirection = Direction.EAST;
							break;
						case SOUTH_WEST:
							instanceVariables.moveDirection = Direction.NORTH_EAST;
							break;
						default:
							instanceVariables.moveDirection = Direction.NORTH;
							break;
						}
						
						HelperMethods.SafeMove(instanceVariables.moveDirection,rc);
						
					}catch (GameActionException ex)
					{
						if(ex.getType() == GameActionExceptionType.CANT_MOVE_THERE)
						{
							instanceVariables.moveDirection = rc.getLocation().directionTo(rc.senseEnemyHQLocation());
						}
					}
				}
				else
				{
					rc.layMine();
				}
				
				while(!rc.isActive()) {
					rc.yield();
				}

				if(instanceVariables.hqLoc.dy == 1 && instanceVariables.lastMoved != Direction.SOUTH)
				{
					if(instanceVariables.hqLoc.dx != 1)
					{
						instanceVariables.lastMoved = Direction.WEST;
						HelperMethods.SafeMove(Direction.WEST, rc);
					}
					else
					{
						instanceVariables.lastMoved = Direction.SOUTH;
						HelperMethods.SafeMove(Direction.SOUTH, rc);
					}
					return;
				}
				
				if(instanceVariables.hqLoc.dx == 1 && instanceVariables.lastMoved != Direction.EAST)
				{
					if(instanceVariables.hqLoc.dy != -1)
					{
						instanceVariables.lastMoved = Direction.SOUTH;
						HelperMethods.SafeMove(Direction.SOUTH, rc);
					}
					else
					{
						instanceVariables.lastMoved = Direction.EAST;
						HelperMethods.SafeMove(Direction.EAST, rc);
					}
					return;
				}
				
				if(instanceVariables.hqLoc.dy == -1 && instanceVariables.lastMoved != Direction.NORTH)
				{
					if(instanceVariables.hqLoc.dx != -1)
					{
						instanceVariables.lastMoved = Direction.EAST;
						HelperMethods.SafeMove(Direction.EAST, rc);
					}
					else
					{
						instanceVariables.lastMoved = Direction.NORTH;
						HelperMethods.SafeMove(Direction.NORTH, rc);
					}
					return;
				}
				
				if(instanceVariables.hqLoc.dx == -1 && instanceVariables.lastMoved != Direction.WEST)
				{
					if(instanceVariables.hqLoc.dy != 1)
					{
						instanceVariables.lastMoved = Direction.NORTH;
						HelperMethods.SafeMove(Direction.NORTH, rc);
					}
					else
					{
						instanceVariables.lastMoved = Direction.WEST;
						HelperMethods.SafeMove(Direction.WEST, rc);
					}
					return;
				}
						
		}
	}
	
}
